package com.my.hp_hr.contorller;

import com.my.hp_hr.entity.Employee;
import com.my.hp_hr.entity.History;
import com.my.hp_hr.service.HistoryService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/employeeGetListServlet")
public class EmployeeListServlet extends HttpServlet {
    private HistoryService historyService = new HistoryService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            HttpSession session = req.getSession();
            String pageNo1 = req.getParameter("pageNo");
            String pageNo = (String) session.getAttribute("pageNo");
            if(pageNo==null){
                pageNo="1";
            }
            if(pageNo1!=null){
                pageNo=pageNo1;
            }
            List<Employee> list = historyService.getWorkServlet(pageNo);
            req.setAttribute("list",list);
            session.setAttribute("pageNo",pageNo);
            //查询总条数
            int count = historyService.getTotal();
            //System.out.println(count);
            req.setAttribute("count",count);
            // 计算总页数
            int totalPage = (int)Math.ceil(count/4.0);
            req.setAttribute("totalPage",totalPage);
            req.getRequestDispatcher("employee_list.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
